2地点の住所からDirection APiを利用してルート距離(m)の計算
library(rjson)
library(RCurl)
## Loading required package: bitops
# ルートの始点と終点の住所をDirections APIのURLに変換
origin.address <- "広島県広島市南区宇品東1-1-71"
destination.address <- "広島県広島市南区霞1-2-3"
origin.address <- curlEscape(iconv(origin.address, to = "UTF-8"))
destination.address <- curlEscape(iconv(destination.address, to = "UTF-8"))
mode <- "walking" # 移動手段(driving,walking,bicycling),ただしbicyclingは米国のみ
url <- paste("http://maps.googleapis.com/maps/api/directions/json?", "origin=",
origin.address, "&destination=", destination.address, "&mode=", mode, "&sensor=false®ion=JP&language=ja",
sep = "")
# Directions APIからJSON情報取得
json <- getURL(url)
# JSONからRに緯度?経度の情報を抽出
rjson <- fromJSON(json)
# 徒歩の道のり距離
dist <- rjson$routes[[1]]$legs[[1]]$distance$value
dist
## [1] 2160
詳細なルート情報
n.step <- length(rjson$routes[[1]]$legs[[1]]$steps)
ext.dir <- function(x) {
s.lat <- rjson$routes[[1]]$legs[[1]]$steps[[x]]$start_location$lat
s.lon <- rjson$routes[[1]]$legs[[1]]$steps[[x]]$start_location$lng
e.lat <- rjson$routes[[1]]$legs[[1]]$steps[[x]]$end_location$lat
e.lon <- rjson$routes[[1]]$legs[[1]]$steps[[x]]$end_location$lng
distance <- rjson$routes[[1]]$legs[[1]]$steps[[x]]$distance$value # meters
duration <- rjson$routes[[1]]$legs[[1]]$steps[[x]]$duration$value # seconds
c(s.lat = s.lat, s.lon = s.lon, e.lat = e.lat, e.lon = e.lon, distance = distance,
duration = duration)
}
route <- t(sapply(1:n.step, ext.dir))
route
## s.lat s.lon e.lat e.lon distance duration
## [1,] 34.37 132.5 34.37 132.5 152 114
## [2,] 34.37 132.5 34.37 132.5 534 389
## [3,] 34.37 132.5 34.37 132.5 16 11
## [4,] 34.37 132.5 34.38 132.5 1129 871
## [5,] 34.38 132.5 34.38 132.5 329 243